package com.zueb.service.impl;

import com.zueb.mapper.FacultyMapper;
import com.zueb.pojo.Classes;
import com.zueb.pojo.Faculty;
import com.zueb.service.FacultyService;
import com.zueb.util.PageUtils;
import com.zueb.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class FacultyServiceImpl implements FacultyService {
    //1. 创建SqlSessionFactory 工厂对象
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public Integer deleteFacultyById(Integer facultyId) {
        //获取SQLSession对象
        SqlSession sqlSession = factory.openSession(true);
        //获取Mapper
        FacultyMapper facultyMapper = sqlSession.getMapper(FacultyMapper.class);
        //调用方法
        int flag = facultyMapper.deleteFacultyById(facultyId);
        //释放资源
        sqlSession.close();
        //返回值
        return flag;
    }

    @Override
    public List<Classes> getClassesByFaculty(Integer facultyId) {
        //获取SQLSession对象
        SqlSession sqlSession = factory.openSession(true);
        //获取Mapper
        FacultyMapper classesMapper = sqlSession.getMapper(FacultyMapper.class);
        //调用方法
        List<Classes> classes = classesMapper.getClassesByFaculty(facultyId);
        //释放资源
        sqlSession.close();
        //返回值
        return classes;
    }

    @Override
    public List<Classes> getClassesByPage(int curPage, int pageSize, int facultyId) {
        //获取SQLSession对象
        SqlSession sqlSession = factory.openSession(true);
        //获取Mapper
        FacultyMapper classesMapper = sqlSession.getMapper(FacultyMapper.class);
        //调用方法
        int index = PageUtils.getIndex(curPage, pageSize);
        List<Classes> classes = classesMapper.getClassesByPage(index, pageSize, facultyId);
        //释放资源
        sqlSession.close();
        //返回值
        return classes;
    }

    @Override
    public int getClassesCount(int facultyId) {
        //获取SQLSession对象
        SqlSession sqlSession = factory.openSession(true);
        //获取Mapper
        FacultyMapper classesMapper = sqlSession.getMapper(FacultyMapper.class);
        //调用方法
        int count = classesMapper.getClassesCount(facultyId);
        //释放资源
        sqlSession.close();
        //返回值

        return count;
    }

    @Override
    public int addClasses(String className, int facultyId) {

        //获取SQLSession对象
        SqlSession sqlSession = factory.openSession(true);
        //获取Mapper
        FacultyMapper classesMapper = sqlSession.getMapper(FacultyMapper.class);
        //调用方法
        int i = classesMapper.addClasses(className, facultyId);
        //释放资源
        sqlSession.close();
        //返回值
        return i;
    }

    @Override
    public int editClasses(int classId, String className, int facultyId) {
        //获取SQLSession对象
        SqlSession sqlSession = factory.openSession(true);
        //获取Mapper
        FacultyMapper classesMapper = sqlSession.getMapper(FacultyMapper.class);
        //调用方法
        int i = classesMapper.editClasses(classId, className, facultyId);
        //释放资源
        sqlSession.close();
        //返回值
        return i;
    }

    @Override
    public Faculty getFacultyById(Integer facultyId) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        FacultyMapper facultyMapper = sqlSession.getMapper(FacultyMapper.class);
        //调用方法
        Faculty faculty = facultyMapper.getFacultyById(facultyId);
        //释放资源
        sqlSession.close();
        //返回查询到的学院
        return faculty;
    }

    @Override
    public Faculty getFacultyByName(String facultyName) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        FacultyMapper facultyMapper = sqlSession.getMapper(FacultyMapper.class);
        //调用方法
        Faculty faculty = facultyMapper.getFacultyByName(facultyName);
        //释放资源
        sqlSession.close();
        //返回查询到的学院
        return faculty;
    }

    @Override
    public int editFaculty(Integer facultyId, String facultyName) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession(true);
        //获取Mapper
        FacultyMapper facultyMapper = sqlSession.getMapper(FacultyMapper.class);
        //调用方法
        int flag = facultyMapper.editFaculty(facultyId, facultyName);
        //释放资源
        sqlSession.close();
        return flag;
    }

    @Override
    public List<Faculty> getFacultyByPage(Integer curPage, Integer pageSize) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        FacultyMapper facultyMapper = sqlSession.getMapper(FacultyMapper.class);
        //调用方法
        int index = PageUtils.getIndex(curPage, pageSize);
        List<Faculty> faculty = facultyMapper.getFacultyByPage(index, pageSize);
        //释放资源
        sqlSession.close();
        //返回查询到的学院
        return faculty;
    }

    @Override
    public int getFacultyCount() {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        FacultyMapper facultyMapper = sqlSession.getMapper(FacultyMapper.class);
        //调用方法
        int count = facultyMapper.getFacultyCount();
        //释放资源
        sqlSession.close();
        //返回查询到的学院
        return count;
    }

    @Override
    public List<Faculty> getAllFaculty() {
        List<Faculty> faculties;
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        FacultyMapper facultyMapper = sqlSession.getMapper(FacultyMapper.class);
        //调用方法
        faculties = facultyMapper.getAllFaculty();
        //释放资源
        sqlSession.close();
        return faculties;
    }

    @Override
    public void addFaculty(Faculty faculty) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession(true);
        //获取Mapper
        FacultyMapper facultyMapper = sqlSession.getMapper(FacultyMapper.class);
        //调用方法
        facultyMapper.addFaculty(faculty);
        //释放资源
        sqlSession.close();
    }
}
